package com.atom.module.picker

import android.content.Context
import android.net.Uri
import java.io.File
import java.util.*

/**
 * # FileType 文件类型
 *
 * 注:
 *
 * - 每一种类型包括的后缀数量有限, 如果下面没有你需要的类型, 可以使用`supplement`动态添加或者直接通过IFileType自定义实现, 详细步骤见Demo
 * - ando.file.core.FileMimeType 中包括大部分的`MimeType`, 可以参照
 *
 * @author javakam
 */
enum class FileType {

    INSTANCE("instance"),
    UNKNOWN(""),
    AUDIO(
        mutableListOf(
            "mp3",
            "flac",
            "ogg",
            "tta",
            "wav",
            "wma",
            "wmv",
            "m3u",
            "m4a",
            "m4b",
            "m4p",
            "mid",
            "mp2",
            "mpga",
            "rmvb"
        )
    ),
    VIDEO(
        mutableListOf(
            "mp4",
            "m3u8",
            "avi",
            "flv",
            "3gp",
            "asf",
            "m4u",
            "m4v",
            "mov",
            "mpe",
            "mpeg",
            "mpg",
            "mpg4"
        )
    ),
    IMAGE(mutableListOf("jpg", "gif", "png", "jpeg", "bmp", "webp")),
    TXT(mutableListOf("txt", "conf", "iml", "ini", "log", "prop", "rc")),
    HTML(mutableListOf("html", "htm", "htmls", "md")),
    PPT(mutableListOf("ppt", "pptx", "pps")),
    EXCEL(mutableListOf("xls", "xlsx")),
    WORD(mutableListOf("doc", "docx")),
    PDF("pdf"),
    CHM("chm"),
    XML("xml"),
    APK("apk"),
    JAR("jar"),
    ZIP("zip");

    private val mTypes: MutableList<String>

    constructor(mime: String) : this(Collections.singletonList(mime))

    constructor(mimes: MutableList<String>) {
        this.mTypes = mimes
    }

    companion object {
        fun FileType.supplement(vararg mimeArray: String): FileType {
            this.mTypes.addAll(mimeArray)
            return this
        }

        fun FileType.remove(vararg mimeType: String): FileType {
            this.mTypes.removeAll(mimeType)
            return this
        }

        fun FileType.dump() {
            mTypes.forEach {
                FileLogger.e("$this : $it")
            }
        }

        /**
         * url:  https://app-xxx-oss/xxx.gif
         *  or
         * fileName:  xxx.gif
         */
        fun fromName(fileName: String?): FileType {
            return fromSuffix(
                FileUtils.getExtension(fileName ?: return UNKNOWN)
            )
        }

        fun fromName(fileName: String?, split: Char): FileType {
            return fromSuffix(
                FileUtils.getExtension(fileName ?: return UNKNOWN, split, false)
            )
        }

        fun fromPath(filePath: String?): FileType {
            if (filePath.isNullOrBlank()) UNKNOWN
            val file = File(filePath ?: return UNKNOWN)
            return if (!file.exists()) UNKNOWN else fromFile(file)
        }

        fun fromFile(file: File): FileType = fromSuffix(FileUtils.getExtension(file.name))

        fun fromUri(context: Context, uri: Uri?): FileType =
            fromSuffix(FileUtils.getExtension(context, uri))

        /**
         * 依据文件扩展名的类型确定相应的MimeType
         */
        private fun fromSuffix(suffix: String): FileType {
            val end = suffix.lowercase(Locale.getDefault())
            values().forEach { t: FileType ->
                if (t.mTypes.contains(end)) return t
            }
            return UNKNOWN
        }
    }

    fun getTypes(): MutableList<String> {
        return mTypes
    }

    fun getMimeType(): String {
        return FileMimeType.getMimeType(this)
    }

}